from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
from poetry.forms import PoemEntryForm
from poetry.models import Poem

def add_poem(request):
  if request.method == 'POST':
    form = PoemEntryForm(request.POST)
    if form.is_valid():
      author = form.clean_data['author']
      title = form.clean_data['title']
      year_of_publication = form.clean_data['year_of_publication']
      text = form.clean_data['text']
      poem = Poem(author = author,
                  title = title,
                  year_of_publication = year_of_publication,
                  text = text)
      poem.save()
      return HttpResponseRedirect('/baudlr/list_poems')
  else:
    form = PoemEntryForm()
  return render_to_response('add_poem.html', { "form" : form })

def list_poems(request):
  poems = Poem.objects.all()
  return render_to_response('list_poems.html', { "poems" : poems })
